# -*- encoding: utf-8 -*-
import importlib.util


class OperationalFunction:
    """
    运行公共方法函数
    """

    @staticmethod
    def operational_function(func):
        """
        运行公共方法函数
        :param func:
        :return:
        """

        class_name = "SysFunction"

        function_name = func

        module_path = "/Users/pengjun/Documents/code/tpweb-server/src/ApiTest/Core/sys_function.py"  # 文件1的相对路径
        module_name = "sys_function"  # 模块名（不含扩展名.py）

        spec = importlib.util.spec_from_file_location(module_name, module_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)

        # 检查给定的类名是否存在于导入的模块中
        if hasattr(module, class_name):
            my_class = getattr(module, class_name)  # 获取类对象

            # 检查给定的函数名是否存在于类中
            if hasattr(my_class, function_name):
                function = getattr(my_class(), function_name)  # 获取函数对象
                res = function()  # 调用函数
                return res
            else:
                return "Function '{function_name}' not found in class '{class_name}'."
        else:
            return f"Class '{class_name}' not found in module '{module_name}'."
